/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iodegree;

import iodegree.lib.pattern.CommNeiStat;
import iodegree.lib.MyTableModel;
import iodegree.lib.CommonsLib;
import iodegree.lib.database.DBHandle;
import iodegree.lib.pattern.Pattern;
import java.util.HashMap;
import javax.swing.JFileChooser;

/**
 *
 * @author Libra
 */
public class CommonNeighborUI extends javax.swing.JFrame {

    /**
     * Creates new form ClusterUI
     */
    public CommonNeighborUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DataFileChooser = new javax.swing.JFileChooser();
        gluGABAGroup = new javax.swing.ButtonGroup();
        jPanelUp = new javax.swing.JPanel();
        lblOpenDatabase = new javax.swing.JLabel();
        btnQryActPatt = new javax.swing.JButton();
        btnOpenDbDefault = new javax.swing.JButton();
        btnOpenDatabase = new javax.swing.JButton();
        rdoGG = new javax.swing.JRadioButton();
        rdoAG = new javax.swing.JRadioButton();
        rdoGA = new javax.swing.JRadioButton();
        rdoAA = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtCycles = new javax.swing.JTextField();
        jPanelDown = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        statsPane = new javax.swing.JScrollPane();
        statsTab = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblOpenDatabase.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblOpenDatabase.setText("Open Database File :");

        btnQryActPatt.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnQryActPatt.setText("Query Pattern");
        btnQryActPatt.setEnabled(false);
        btnQryActPatt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQryActPattActionPerformed(evt);
            }
        });

        btnOpenDbDefault.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnOpenDbDefault.setText("Default");
        btnOpenDbDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDbDefaultActionPerformed(evt);
            }
        });

        btnOpenDatabase.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnOpenDatabase.setText("Open");
        btnOpenDatabase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDatabaseActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoGG);
        rdoGG.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoGG.setSelected(true);
        rdoGG.setText("Glu>Glu");
        rdoGG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoGGActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoAG);
        rdoAG.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoAG.setText("GABA>Glu");
        rdoAG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoAGActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoGA);
        rdoGA.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoGA.setText("Glu>GABA");
        rdoGA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoGAActionPerformed(evt);
            }
        });

        gluGABAGroup.add(rdoAA);
        rdoAA.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdoAA.setText("GABA>GABA");
        rdoAA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoAAActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Random Sim :");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Cycles");

        txtCycles.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtCycles.setText("100000");
        txtCycles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCyclesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelUpLayout = new javax.swing.GroupLayout(jPanelUp);
        jPanelUp.setLayout(jPanelUpLayout);
        jPanelUpLayout.setHorizontalGroup(
            jPanelUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUpLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelUpLayout.createSequentialGroup()
                        .addComponent(lblOpenDatabase)
                        .addGap(18, 18, 18)
                        .addComponent(rdoGG)
                        .addGap(18, 18, 18)
                        .addComponent(rdoGA)
                        .addGap(18, 18, 18)
                        .addComponent(rdoAG)
                        .addGap(18, 18, 18)
                        .addComponent(rdoAA))
                    .addGroup(jPanelUpLayout.createSequentialGroup()
                        .addComponent(btnOpenDatabase)
                        .addGap(18, 18, 18)
                        .addComponent(btnOpenDbDefault)
                        .addGap(105, 105, 105)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(txtCycles, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(btnQryActPatt)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelUpLayout.setVerticalGroup(
            jPanelUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUpLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(rdoGG)
                        .addComponent(rdoGA)
                        .addComponent(rdoAG)
                        .addComponent(rdoAA))
                    .addComponent(lblOpenDatabase))
                .addGap(18, 18, 18)
                .addGroup(jPanelUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnOpenDatabase)
                    .addComponent(btnOpenDbDefault)
                    .addComponent(txtCycles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(btnQryActPatt))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        statsTab.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        statsPane.setViewportView(statsTab);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanelDown.addTab("Statistics", jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelDown)
                    .addComponent(jPanelUp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelUp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelDown)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOpenDbDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDbDefaultActionPerformed
        String pathToFile = CommonsLib.getDefaultFile();

        grpList = getGrpList(pathToFile, 0);
        connMap = getConnMap(pathToFile, 0);
        btnQryActPatt.setEnabled(true);

    }//GEN-LAST:event_btnOpenDbDefaultActionPerformed

    private void btnOpenDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDatabaseActionPerformed
        int returnVal = DataFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String pathToFile = DataFileChooser.getSelectedFile().getAbsolutePath();
//            System.out.println("File " + pathToFile + " opened.");
            grpList = getGrpList(pathToFile, 0);
            connMap = getConnMap(pathToFile, 0);
            btnQryActPatt.setEnabled(true);
        }
    }//GEN-LAST:event_btnOpenDatabaseActionPerformed

    private void btnQryActPattActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQryActPattActionPerformed
        getCommonNeighborStats(grpList, connMap);
//        D.p("finished");
    }//GEN-LAST:event_btnQryActPattActionPerformed

    private void txtCyclesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCyclesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCyclesActionPerformed

    private void rdoGGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoGGActionPerformed
        type = 0;
    }//GEN-LAST:event_rdoGGActionPerformed

    private void rdoGAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoGAActionPerformed
        type = 1;
    }//GEN-LAST:event_rdoGAActionPerformed

    private void rdoAGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoAGActionPerformed
        type = 2;
    }//GEN-LAST:event_rdoAGActionPerformed

    private void rdoAAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoAAActionPerformed
        type = 3;
    }//GEN-LAST:event_rdoAAActionPerformed

    private int[][] getGrpList(String pathToFile, int div) {
        DBHandle db = new DBHandle();
        return db.sizeNIdForSplit(pathToFile, div);
    }

    private HashMap<Integer, Integer> getConnMap(String pathToFile, int div) {
        DBHandle db = new DBHandle();
        int[][] connList = db.connType(pathToFile, div);

        HashMap<Integer, Integer> map = new HashMap<>(connList.length * 2);
        for (int i = 0; i < connList.length; i++) {
            map.put((connList[i][0] << 12) + connList[i][1], connList[i][2]);
        }
        return map;
    }

    private void getCommonNeighborStats(int[][] grpList, HashMap<Integer, Integer> connMap) {
        Pattern pattern = new Pattern();
        CommNeiStat stats;
        stats = pattern.commonNeighbor(grpList, connMap);
//        int[][] sum = {stats.getGG(), stats.getGA(), stats.getAG(), stats.getAA()};
        int cycles = Integer.parseInt(txtCycles.getText());
        stats.setType(type);
        stats.setCycles(cycles);
        Object[][] tbData = new Object[5][9];
        for (int i = 0; i < 5; i++) {
            tbData[i] = new Object[9];
        }
        tbData[1][0] = "n";
        tbData[2][0] = "Prob";
        tbData[3][0] = "S.E.M";
        tbData[4][0] = "p";
        System.arraycopy(stats.getTitle(), 0, tbData[0], 1, 8);
        System.arraycopy(stats.getN(), 0, tbData[1], 1, 8);
        System.arraycopy(stats.getProb(), 0, tbData[2], 1, 8);
        System.arraycopy(stats.getSem(), 0, tbData[3], 1, 8);
        System.arraycopy(stats.getP(), 0, tbData[4], 1, 8);


        MyTableModel tabM = new MyTableModel(tbData);
        statsPane.setColumnHeaderView(null);
        statsTab.setModel(tabM);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CommonNeighborUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CommonNeighborUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser DataFileChooser;
    private javax.swing.JButton btnOpenDatabase;
    private javax.swing.JButton btnOpenDbDefault;
    private javax.swing.JButton btnQryActPatt;
    private javax.swing.ButtonGroup gluGABAGroup;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jPanelDown;
    private javax.swing.JPanel jPanelUp;
    private javax.swing.JLabel lblOpenDatabase;
    private javax.swing.JRadioButton rdoAA;
    private javax.swing.JRadioButton rdoAG;
    private javax.swing.JRadioButton rdoGA;
    private javax.swing.JRadioButton rdoGG;
    private javax.swing.JScrollPane statsPane;
    private transient javax.swing.JTable statsTab;
    private javax.swing.JTextField txtCycles;
    // End of variables declaration//GEN-END:variables
//    private String pathToFile;
//    private int div = 0;
    private int type = 0;
    private int[][] grpList;
    private HashMap<Integer, Integer> connMap;
}
